﻿using System;
using System.Collections.Generic;

using System.Text;
using MVCFoto.IDAL;
using MVCFoto.Model;
using System.IO;

namespace MVCFoto.BLL
{
    public class Photo
    {
        private static readonly IPhoto dal = MVCFoto.DALFactory.Factory.Create<IPhoto>("Photo");
        /// <summary>
        /// 取得用户的相册列表
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="privacy">允许查看的最高权限值</param>
        /// <returns></returns>
        public IList<AlbumInfo> GetUserAlbums(int userId, AlbumPrivacy privacy, int page, int pagesize, out int itemCount)
        {
            return dal.GetUserAlbums(userId, (short)privacy,page,pagesize,out itemCount);
        }

        public int CreateAlbum(AlbumInfo album)
        {
            return dal.CreateAlbum(album);
        }
        public int CreateAlbum(string title, string desc, DateTime photodate, short privacy)
        {
            AlbumInfo album = new AlbumInfo();
            album.CoverPhoto = "";
            album.CreateDate = DateTime.Now;
            album.PhotoDate = photodate;
            album.Title = title;
            album.Desc = desc;
            album.UserID = User.MyID;
            album.Privacy = privacy;
            return CreateAlbum(album);
        }
        public AlbumInfo GetAlbum(int albumId)
        {
            return dal.GetAlbum(albumId);
        }
        public AlbumInfo GetAlbum(int userId, string title)
        {
            return dal.GetAlbum(userId,title);
        }
        public bool UpdateAlbumCover(int albumId, string cover)
        {
            return dal.UpdateAlbumCover(albumId, cover);
        }
        public int AddPhoto(PhotoInfo photo)
        {           
            int photoId = dal.AddPhoto(photo);           
            return photoId;
        }
        public bool SetPhotoAsCover(int photoId)
        {
            PhotoInfo photo = dal.GetPhoto(photoId,false);
            if (photo != null)
            {
                AlbumInfo album = dal.GetAlbum(photo.AlbumID);
                if (album != null)
                {
                    if (album.UserID != User.MyID)
                    {
                        return false;
                    }
                    else
                    {
                        if (Graphic.Thumbnail.ThumbnailCover(System.Web.HttpContext.Current.Server.MapPath(photo.Photo)))
                        {
                            string cover = Graphic.Thumbnail.GetCoverPhotoUrl(photo.Photo);
                            //保存封面
                            return dal.UpdateAlbumCover(album.AlbumID,cover);
                        }
                    }
                }
            }
            return false;
        }
        public IList<PhotoInfo> GetAlbumPhotos(int albumId, int page, int pagesize)
        {
            return dal.GetAlbumPhotos(albumId, page, pagesize);
        }
        public PhotoInfo GetPhoto(int photoId)
        {
            return GetPhoto(photoId, false);
        }
        public PhotoInfo GetPhoto(int photoId, bool updateViews)
        {
            string session_key = string.Format("s_photo_views_{0}", photoId);
            if (updateViews && System.Web.HttpContext.Current.Session[session_key] == null)
            {                
                System.Web.HttpContext.Current.Session[session_key] = true;
            }
            else
                //防止刷views
                updateViews = false;

            return dal.GetPhoto(photoId, updateViews);
        }
        public bool UpdatePhoto(PhotoInfo photo)
        {
            return dal.UpdatePhoto(photo);
        }
        /// <summary>
        /// 更新照片坐标
        /// </summary>
        /// <param name="photoId"></param>
        /// <param name="lat"></param>
        /// <param name="lng"></param>
        /// <returns></returns>
        public bool UpdatePhotoLatLng(int photoId, string lat, string lng)
        {
            return dal.UpdatePhotoLatLng(photoId, lat, lng);
        }
        public bool UpdatePhoto(int photoId, string title, string desc)
        {
            return dal.UpdatePhoto(new PhotoInfo { PhotoID=photoId,Title=title, Desc=desc });
        }
        public bool UpdateAlbum(AlbumInfo album)
        {
            return dal.UpdateAlbum(album);
        }
        public IList<PhotoInfo> GetUserNewPhotos(int userId, AlbumPrivacy privacy)
        {
            return dal.GetUserNewPhotos(userId, (short)privacy);
        }
        /// <summary>
        /// 取得带指定tag的照片
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="tagName"></param>
        /// <param name="page"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public IList<PhotoInfo> GetTagedPhotos(int userId, string tagName,int page, int pagesize, out int itemcount)
        {           
            BLL.Photo.AlbumPrivacy privacy = AlbumPrivacy.Public;
            if(BLL.User.MyID == userId)
                privacy = AlbumPrivacy.Private;
            else if(BLL.Friend.IsUserFriend(userId))
                privacy = AlbumPrivacy.Friend;

            return dal.GetTagedPhotos(userId, tagName,(short)privacy,page, pagesize,out itemcount);
        }
        /// <summary>
        /// 删除照片
        /// </summary>
        /// <param name="photoId"></param>
        /// <returns></returns>
        public bool DeletePhoto(int photoId)
        {           
            PhotoInfo photo = GetPhoto(photoId);
            if (photo != null)
            {
                string photofile = photo.Photo;
                if (dal.DeletePhoto(photoId))
                {
                    return DeletePhotoFiles(photofile);
                }
                
            }
            return false;
        }
        public bool DeletePhotoFiles(string filepath)
        {//TODO:封面目录是否删除
            filepath = System.Web.HttpContext.Current.Server.MapPath(filepath);
            if (File.Exists(filepath))
            {
                string dir = Path.GetDirectoryName(filepath);
                string filename = Path.GetFileNameWithoutExtension(filepath);
                //删除所有同名文件，包括子目录
                foreach (string f in Directory.GetFiles(dir, filename + ".*", SearchOption.AllDirectories))
                {
                    File.Delete(f);
                }
            }
            return true;
        }

        /// <summary>
        /// 根据Ids取得照片
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="Ids">逗号分隔的照片Id列表</param>
        /// <returns></returns>
        public IList<PhotoInfo> GetPhotosByIds(int userId,string Ids)
        {
            return dal.GetPhotosByIds(userId,Ids);
        }
        public enum AlbumPrivacy
        { 
            Public = 0,
            Friend = 1,
            Private = 2
        }
    }
}
